import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @作者 ： pwx5328113 @时间： 2023/3/6
 * @测试点：jdbc连接参数autoBalance为leastconn，minReservedConPerCluster参数异常，正常报错
 **/
public class JDBC_QuikAutoBalance_Case0006 {
	public static void main(String[] args) throws Exception {
		System.out.println("----------start----------");
		String err1_msg = null, err2_msg = null, err3_msg = null, err4_msg = null;
		List<Connection> connections = new ArrayList<>();
		System.out.println("----------point1：连接参数minReservedConPerCluster包含字符串----------");
		String url1 = "jdbc:postgresql://" + args[0] + "/" + args[1]
				+ "?loggerLevel=info&autoBalance=leastconn&minReservedConPerCluster=da10";
		try {
			Connection connection = DriverManager.getConnection(url1, args[2], args[3]);
			connections.add(connection);
		} catch (SQLException e) {
			err1_msg = e.getMessage();
			System.out.println(err1_msg);
		}
		System.out.println("----------point2：连接参数minReservedConPerCluster为小数----------");
		String url2 = "jdbc:postgresql://" + args[0] + "/" + args[1]
				+ "?loggerLevel=info&autoBalance=leastconn&minReservedConPerCluster=10.2";
		try {
			Connection connection = DriverManager.getConnection(url2, args[2], args[3]);
			connections.add(connection);
		} catch (SQLException e) {
			err2_msg = e.getMessage();
			System.out.println(err2_msg);
		}
		System.out.println("----------point3：连接参数minReservedConPerCluster为负数----------");
		String url3 = "jdbc:postgresql://" + args[0] + "/" + args[1]
				+ "?loggerLevel=info&autoBalance=leastconn&minReservedConPerCluster=-12";
		try {
			Connection connection = DriverManager.getConnection(url3, args[2], args[3]);
			connections.add(connection);
		} catch (SQLException e) {
			err3_msg = e.getMessage();
			System.out.println(err3_msg);
		}
		System.out.println("----------point4：连接参数minReservedConPerCluster配置超过上限值100----------");
		String url4 = "jdbc:postgresql://" + args[0] + "/" + args[1]
				+ "?loggerLevel=info&autoBalance=leastconn&minReservedConPerCluster=101";
		try {
			Connection connection = DriverManager.getConnection(url4, args[2], args[3]);
			connections.add(connection);
		} catch (SQLException e) {
			err4_msg = e.getMessage();
			System.out.println(err4_msg);
		}
		System.out.println("----------sleep 10s----------");
		TimeUnit.SECONDS.sleep(10); // sleep过程中数据库节点查询建连数量
		String err1_flag = "Parameter minReservedConPerCluster=da10 parsed failed, value range: int && [0, 100].";
		String err2_flag = "Parameter minReservedConPerCluster=10.2 parsed failed, value range: int && [0, 100].";
		String err3_flag = "Parameter minReservedConPerCluster=-12 parsed failed, value range: int && [0, 100].";
		String err4_flag = "Parameter minReservedConPerCluster=101 parsed failed, value range: int && [0, 100].";
		if (err1_msg.indexOf(err1_flag) != -1 && err2_msg.indexOf(err2_flag) != -1 && err3_msg.indexOf(err3_flag) != -1
				&& err4_msg.indexOf(err4_flag) != -1) {
			System.out.println("----The verification is correct.----");
		}
	}
}
